package com.zelin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.webkit.graphics.WCImageFrame;
import com.zelin.mapper.ClassesMapper;
import com.zelin.mapper.StudentMapper;
import com.zelin.pojo.PageResult;
import com.zelin.pojo.Student;
import com.zelin.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/9/14-15:16
 * ------------------------------
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;
    //1. 查询所有学生
    @Override
    public List<Student> findAll() {
        //1.1 查询出所有学生集合
        List<Student> students = studentMapper.selectByExample(null);
        //1.2 处理查询出的学生
        for (Student student : students) {
            //1.2.1 得到班级名称
            String cname = classesMapper.selectByPrimaryKey(student.getCid()).getCname();
            //1.2.2 设置班级名称
            student.setCname(cname);
        }
        //1.3 返回
        return students;
    }

    /**
     * 功能: 分页查询
     * 参数:
     * 返回值: com.zelin.pojo.PageResult<com.zelin.pojo.Student>
     * 时间: 2021/9/14 15:57
     */
    @Override
    public PageResult<Student> findByPage(int page, int pageSize) {
        //1. 开始分页
        PageHelper.startPage(page,pageSize);
        //2. 开始查询
        //2.1 定义查询实例
        Example example = new Example(Student.class);
        //Example.Criteria criteria = example.createCriteria();

        //2.2 开始实例查询
        List<Student> students = studentMapper.selectByExample(example);
        //2.2.1 处理查询出的学生
        for (Student student : students) {
            //① 得到班级名称
            String cname = classesMapper.selectByPrimaryKey(student.getCid()).getCname();
            //② 设置班级名称
            student.setCname(cname);
        }
        //3. 将查询 结果转换为Page<T> 对象
        Page<Student> studentPage = (Page<Student>) students;
        //4. 返回 pageResult对象
        return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
    }

    /**
     * 功能: 条件查询带分页
     * 参数:
     * 返回值: com.zelin.pojo.PageResult<com.zelin.pojo.Student>
     * 时间: 2021/9/14 16:18
     */
    @Override
    public PageResult<Student> search(int page, int pageSize, Student student) {
        //1. 开始分页
        PageHelper.startPage(page,pageSize);
        //2. 开始查询
        //2.1 定义查询实例
        Example example = new Example(Student.class);
        //2.2.1 定义查询条件
        Example.Criteria criteria = example.createCriteria();
        //2.2 添加查询条件
        if(student != null){
            if(!StringUtils.isEmpty(student.getSname())){
                criteria.andLike("sname","%" + student.getSname()  + "%");
            }
            if(!StringUtils.isEmpty(student.getAddr())){
                criteria.andLike("addr","%" + student.getAddr() + "%");
            }
        }
        //2.3 开始实例条件查询
        List<Student> students = studentMapper.selectByExample(example);
        //2.3.1 处理查询出的学生
        for (Student st : students) {
            //① 得到班级名称
            String cname = classesMapper.selectByPrimaryKey(st.getCid()).getCname();
            //② 设置班级名称
            st.setCname(cname);
        }
        //3. 将查询 结果转换为Page<T> 对象
        Page<Student> studentPage = (Page<Student>) students;
        //4. 返回 pageResult对象
        return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
    }

    /**
     * 功能: 添加学生
     * 参数:
     * 返回值: void
     * 时间: 2021/9/15 15:41
     */
    @Override
    public void add(Student student) {
        studentMapper.insert(student);
    }

    /**
     * 功能: 修改学生
     * 参数:
     * 返回值: void
     * 时间: 2021/9/15 15:41
     */
    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    /**
     * 功能: 删除学生
     * 参数:
     * 返回值: void
     * 时间: 2021/9/15 16:22
     */
    @Override
    public void delete(Integer sid) {
        studentMapper.deleteByPrimaryKey(sid);
    }
}
